import { mapState, mapMutations } from "vuex";
import cStoreList from "components/storeList";
import cGoodsList from "components/goodsList";
import loading from "components/loading";
import { scrollMixin } from "utils/mixin";
import optionMenu from "./child/option_menu";
import yzBlank from "components/ui_components/yz_blank";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      title: "搜索",
      inputs: "",
      amout: false,
      stores: [],
      order_by: "",
      order_field: "",
      showPage: false,
      city: "",
      point: {},
      screenMenu: [{
        name: "商品",
        api: "plugin.store-cashier.frontend.store.goods.get-store-goods-list"
      },{
        name: "门店",
        api: "plugin.store-cashier.frontend.store.goods.get-store-goods-by-title"
      }],
      screenIndex: 1,
      screenShow: false,
      listData:[],

      //more
      isLoadMore: true,
      page: 1,
      total_page: 0,
      openTemplate: 0, //门店列表样式
      is_sales_display: 0, //门店 is_sales_display  0 不显示销量  1显示销量
    };
  },
  computed: mapState(["view"]),
  ...mapMutations(["views"]),
  mounted() {
    this.stores = [];
    this.listData = [];
    this.page = 1;
    this.total_page = 0;
    this.isLoadMore = true;
    this.screenShow = false;
  },
  methods: {
    setScreenShow(){
      this.screenShow = !this.screenShow;
    },
    enterSearch(event) {
      if (event.keyCode === 13) {
        //如果按的是enter键
        event.preventDefault();
        this.gotoSearch();
      }
    },
    goback() {
      this.$router.go(-1);
    },
    searchSet(e){
      this.screenIndex = e;
      if (this.$route.params.city && this.$route.params.city !== "1") {
        this.city = this.$route.params.city;
        this.point = JSON.parse(this.$route.params.point);
        this.gotoSearch();
      } else {
        this.fun
          .getLocation()
          .then(res => {
            this.city = res.city;
            this.point = res.point;
            this.gotoSearch();
          })
          .catch(err => {
            this.gotoSearch();
            console.log(err);
          });
      }
    },
    setScreenIndex(e){
      this.screenIndex = e;
      this.screenShow = !this.screenShow;
      this.gotoSearch();
    },
    gotoSearch() {
      this.showPage = false;
      this.page = 1;
      let p = this.fun.bd_encrypt(this.point.lng, this.point.lat);
      let urlstr = this.screenMenu[this.screenIndex].api;
      let json={};
      if(this.screenIndex == 0){
        json = {
          search:{keyword:this.inputs},
          lng: p.lng,
          lat: p.lat,
        }
        $http.post(urlstr, json).then(response => {
          this.showPage = true;
          if (response.result === 1) {
            this.isLoadMore = true;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.listData = response.data.data
          } else {
            this.stores = [];
          }
        });
      }else{
        json = {
          page: this.page,
          kwd: this.inputs,
          lng: p.lng,
          lat: p.lat,
          city_name: this.city
        };
        $http.get(urlstr, json).then(response => {
          this.showPage = true;
          if (response.result === 1) {
            this.isLoadMore = true;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.stores = response.data.data;
            this.openTemplate = response.data.store_list_style || 0;
            this.is_sales_display = response.data.is_sales_display;
          } else {
            this.stores = [];
          }
        });
      }

    },
    //获取更多数据
    getMoreData() {
      if (this.$refs.ref_optionMenu.isOpenMoreChild) {
        this.$refs.ref_optionMenu.getChildrenMoreData();
        return;
      }
      let p = this.fun.bd_encrypt(this.point.lng, this.point.lat);
      let urlstr = this.screenMenu[this.screenIndex].api;
      let json={};
      if(this.screenIndex == 0){
        json = {
          search:{keyword:this.inputs},
          lng: p.lng,
          lat: p.lat,
        }
      }else{
        json = {
          kwd: this.inputs,
          lng: p.lng,
          lat: p.lat,
          city_name: this.city
        };
      }

      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        json.page = this.page;
        $http.get(urlstr, json, "加载中").then(
          response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              var myData = response.data.data;
              if(this.screenIndex == 0){
                this.listData = this.listData.concat(myData);
              }else{
                this.stores = this.stores.concat(myData); //数组拼接
              }
              this.is_sales_display = response.data.is_sales_display;
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            // error callback
          }
        );
      }
    }
  },

  components: { cStoreList, loading, optionMenu, yzBlank,cGoodsList }
};
